<template>
  <!-- 表单 -->
  <el-form ref="employeeForm" :model="employeeForm" :rules="rules" label-width="120px">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="employeeForm.username" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="employeeForm.mobile" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="employeeForm.timeOfEntry" placeholder="请选择入职时间" />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="employeeForm.formOfEmployment" placeholder="请选择">
        <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="employeeForm.workNumber" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input v-model="employeeForm.departmentName" placeholder="请选择部门" @focus="chooseDept" />
      <!-- 树形 -->
      <div v-if="showTree" class="tree">
        <el-tree default-expand-all :data="treeData" :props="defaultProps" @node-click="handleNodeClick" />
      </div>
    </el-form-item>
    <el-form-item label="转正时间">
      <el-date-picker v-model="employeeForm.correctionTime" placeholder="请选择转正时间" />
    </el-form-item>
    <el-form-item>
      <el-button size="mini" @click="cancel">取消</el-button>
      <el-button size="mini" type="primary" @click="submit">确定</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import employeeNum from '@/constant/employees'
import { getDepartments } from '@/api/department'
import { transTree } from '@/utils'
import { addEmployee } from '@/api/employee'
export default {
  name: 'EmployeeForm',
  data() {
    return {
      employeeForm: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      },
      hireType: employeeNum.hireType, // 聘用形式数组
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showTree: false
    }
  },
  created() {

  },
  methods: {
    // 重置数据及表单
    resetForm() {
      // 重置数据
      this.employeeForm = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 重置表单
      this.$refs.employeeForm.resetFields()
    },
    // 选择部门
    chooseDept() {
      this.getDepartments()
    },

    // 获取部门信息
    async getDepartments() {
      const res = await getDepartments()
      // console.log(res)
      this.showTree = true
      this.treeData = transTree(res.data.depts, '')
    },

    // 点击节点触发
    handleNodeClick(data) {
      console.log(data)
      // 有子节点不能点击
      if (data && data.children) return
      this.employeeForm.departmentName = data.name
      this.showTree = false
    },

    // 点击确认按钮
    submit() {
      // 兜底校验
      this.$refs.employeeForm.validate(async valid => {
        if (!valid) return
        const res = await addEmployee(this.employeeForm)
        // console.log(res)
        // 提示
        this.$message.success(res.message)
        // 关闭弹框
        this.$emit('update')
      })
      //
      // this.$emit('close')
    },

    // 点击取消按钮
    cancel() {
      // 关闭弹框
      this.$emit('close')
    }
  }
}
</script>
<style scoped lang="scss">
.el-date-editor.el-input, .el-date-editor.el-input__inner {
    width: 100% !important;
}
.el-select {
    width: 100% !important;
}
form.el-form {
    padding: 0 80px 0 40px;
}
</style>
